<template>
	<view style="padding: 30rpx;">
		<button @click="callStorage()">setStorage</button>
		<button @click="callGetStorage()">获取缓存</button>
		<button @click="callStoInfo()">获取字段列表</button>
		<button @click="callRemove()" type="warn">删除</button>
		<button @click="callClear()" type="warn">清空</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				
			}
		},
		methods: {
			callStorage(){
				console.log("callStorage-->userName yanghuibing");
				uni.setStorage({
					key:"userName",
					data:"yanghuibing",
					success() {
						console.log("存储userName成功回调");
						uni.showToast({
							title: '存储成功',
							icon: 'none'
						})
					},
					fail() {
						console.error("存储userName失败");
						uni.showToast({
							title: '存储失败',
							icon: 'none'
						})
					}
				})
			},
			callGetStorage(){
				uni.getStorage({
					key:"userName",
					success:function(result) {
						console.log("获取本地缓存成功: " + result.data);
						uni.showToast({
							title: '获取成功: ' + result.data,
							icon: 'none'
						})
					},
					fail:function(result) {
						console.error("获取本地缓存失败: " + result.errMsg); 
						uni.showToast({
							title: '获取失败',
							icon: 'none'
						})
					}
				})
			},
			callStoInfo(){
				console.log("callStoInfo-->");
				uni.getStorageInfo({
					success:function(result){
						console.log("缓存信息: " + JSON.stringify(result));
						console.log("缓存键列表: " + result.keys);
						uni.showToast({
							title: '共' + result.keys.length + '个缓存项',
							icon: 'none'
						})
					},
					fail:function(err) {
						console.error("获取缓存信息失败: " + err.errMsg);
						uni.showToast({
							title: '获取信息失败',
							icon: 'none'
						})
					}
				})
			},
			callRemove(){
				uni.removeStorage({
					key:"userName",
					success: ()=>{
						console.log("删除userName成功");
						uni.showToast({
							title: '删除成功',
							icon: 'none'
						})
					},
					fail:(err)=>{
						console.error("删除userName失败: " + err.errMsg);
						uni.showToast({
							title: '删除失败',
							icon: 'none'
						})
					}
				})
			},
			callClear(){
				uni.clearStorage({
					success: ()=>{
						console.log("清空缓存成功");
						uni.showToast({
							title: '清空成功',
							icon: 'none'
						})
					},
					fail: (err)=>{
						console.error("清空缓存失败: " + err.errMsg);
						uni.showToast({
							title: '清空失败',
							icon: 'none'
						})
					}
				})
			}
		}
	}
</script>

<style>
button{
	margin: 20rpx;
}
</style>